package com.ruoyi.baselibrary.infobaseindex.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.sound.sampled.Line;

import com.ruoyi.baselibrary.infobasecategory.domain.InfoBaseCategory;
import com.ruoyi.baselibrary.infobasecategory.service.IInfoBaseCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.baselibrary.infobaseindex.domain.InfoBaseIndex;
import com.ruoyi.baselibrary.infobaseindex.service.IInfoBaseIndexService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基础库-指标库Controller
 *
 * @author ruoyi
 * @date 2022-01-17
 */
@RestController
@RequestMapping("/infobaseindex/InfoBaseIndex")
public class InfoBaseIndexController extends BaseController {
    @Autowired
    private IInfoBaseIndexService infoBaseIndexService;
    @Autowired
    private IInfoBaseCategoryService baseCategoryService;

    /**
     * 查询基础库-指标库列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InfoBaseIndex infoBaseIndex) {
        String baseCategoryByParentIds = baseCategoryService.getBaseCategoryByParentId(infoBaseIndex.getCategory());
        infoBaseIndex.setCategoryIds(baseCategoryByParentIds);
        infoBaseIndex.setCategory(null);
        startPage();
        List<InfoBaseIndex> list = infoBaseIndexService.selectInfoBaseIndexList(infoBaseIndex);
        for (InfoBaseIndex baseIndex : list) {
            baseIndex.setCategoryName(baseCategoryService.getBaseCategoryName(baseIndex.getCategory()));
        }
        return getDataTable(list);
    }

    /**
     * 导出基础库-指标库列表
     */
    @PreAuthorize("@ss.hasPermi('infoBaseIndex:infoBaseIndex:export')")
    @Log(title = "基础库-指标库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoBaseIndex infoBaseIndex) {
        List<InfoBaseIndex> list = infoBaseIndexService.selectInfoBaseIndexList(infoBaseIndex);
        ExcelUtil<InfoBaseIndex> util = new ExcelUtil<InfoBaseIndex>(InfoBaseIndex.class);
        util.exportExcel(response, list, "基础库-指标库数据");
    }

    /**
     * 获取基础库-指标库详细信息
     */
    @PreAuthorize("@ss.hasPermi('infoBaseIndex:infoBaseIndex:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoBaseIndexService.selectInfoBaseIndexById(id));
    }

    /**
     * 新增基础库-(部门)指标库
     */
    @PreAuthorize("@ss.hasPermi('infoBaseIndex:infoBaseIndex:add')")
    @Log(title = "基础库-(部门)指标库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoBaseIndex infoBaseIndex) {
        return toAjax(infoBaseIndexService.insertInfoBaseIndex(infoBaseIndex));
    }

    /**
     * 新增基础库-(项目)指标库
     */
    @Log(title = "基础库-(项目)指标库", businessType = BusinessType.INSERT)
    @RequestMapping("project")
    public AjaxResult addProject(@RequestBody InfoBaseIndex infoBaseIndex) {
        return toAjax(infoBaseIndexService.insertInfoBaseIndexs(infoBaseIndex));
    }

    /**
     * 修改基础库-指标库
     */
    @PreAuthorize("@ss.hasPermi('infoBaseIndex:infoBaseIndex:edit')")
    @Log(title = "基础库-指标库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoBaseIndex infoBaseIndex) {
        return toAjax(infoBaseIndexService.updateInfoBaseIndex(infoBaseIndex));
    }

    /**
     * 删除基础库-指标库
     */
    @PreAuthorize("@ss.hasPermi('infoBaseIndex:infoBaseIndex:remove')")
    @Log(title = "基础库-指标库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoBaseIndexService.deleteInfoBaseIndexByIds(ids));
    }

   /* *//**
     * 查询项目或部门的指标体系
     *//*
    @GetMapping("/cxlist")
    public TableDataInfo cxlist(InfoBaseIndex infoBaseIndex) {
        String baseCategoryByParentIds = baseCategoryService.getBaseCategoryByParentId(infoBaseIndex.getCategory());
        infoBaseIndex.setCategoryIds(baseCategoryByParentIds);
        infoBaseIndex.setCategory(null);
        startPage();
        //list查询到指标库中的所有指标文件
        List<InfoBaseIndex> list = infoBaseIndexService.selectInfoBaseIndexList(infoBaseIndex);

        List<InfoBaseIndex> list1=new ArrayList<>();
        //判断后在循环1为部门，2为项目
        if("1".equals(infoBaseIndex.getSjs())){
            //找出部门的list
            for(int i=0;i<list.size();i++){
                Long category= list.get(i).getCategory();
               InfoBaseCategory infoBaseCategory= baseCategoryService.selectInfoBaseCategoryById(category);
                Long parentId=infoBaseCategory.getParentId();
               //41部门
               if(41==parentId){
                   for (InfoBaseIndex baseIndex : list1) {
                       baseIndex.setCategoryName(baseCategoryService.getBaseCategoryName(baseIndex.getCategory()));
                   }
                   list1.add(list.get(i));
               }
            }
        }else {
            //从list中找出部门的list
            for(int i=0;i<list.size();i++){
                Long category= list.get(i).getCategory();
                InfoBaseCategory infoBaseCategory= baseCategoryService.selectInfoBaseCategoryById(category);
                Long parentId=infoBaseCategory.getParentId();
                //41项目
                if(42==parentId || 40==parentId){
                    for (InfoBaseIndex baseIndex : list1) {
                        baseIndex.setCategoryName(baseCategoryService.getBaseCategoryName(baseIndex.getCategory()));
                    }
                    list1.add(list.get(i));
                }
            }
        }
//        String fileName=infoBaseIndex.getFileName();
//        if(fileName!=null){
//            infoBaseIndex.setFileName(fileName);
//            list1=infoBaseIndexService.selectInfoBaseIndexList(infoBaseIndex);
//        }


        return getDataTable(list);
    }*/

    /**
     * 查询项目或部门的指标体系
     */
    @GetMapping("/cxlist")
    public TableDataInfo getIndexList(InfoBaseIndex infoBaseIndex) {
        startPage();
        //list查询到指标库中的所有指标文件
        List<InfoBaseIndex> list = infoBaseIndexService.getIndexList(infoBaseIndex);
        return getDataTable(list);
    }

    /**
     * 修改基础库-指标库-文件名称
     */
    @PostMapping("/editIndexName")
    public AjaxResult editIndexName(@RequestBody InfoBaseIndex infoBaseIndex) {
        return toAjax(infoBaseIndexService.editInfoBaseIndexName(infoBaseIndex));
    }
}
